/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.mrp.dao.hibernate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.erp.prod.mrp.dao.ProdMrpBillRDao;
import com.erp.prod.mrp.dao.model.ProdMrpBillR;
import com.erp.prod.mrp.dao.model.ProdMrpBillRCO;

@Repository
public class ProdMrpBillRDaoImpl implements ProdMrpBillRDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(ProdMrpBillR obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(ProdMrpBillR obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(ProdMrpBillR obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(ProdMrpBillR obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<ProdMrpBillR> getDataObjects() {
        return this.daoSupport.getDataAllObject(ProdMrpBillR.class);
    }

    @Override
    public ProdMrpBillR getDataObject(int id) {
        return (ProdMrpBillR)this.daoSupport.getDataObject(ProdMrpBillR.class, id);
    }
    
    @Override
    public ProdMrpBillR getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<ProdMrpBillR> getDataObjects(ProdMrpBillRCO paramObj) {
        return null;
    }
    
    @Override
    public List<ProdMrpBillR> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<ProdMrpBillR> getDataObjects(Pages pages, ProdMrpBillRCO paramObj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, ProdMrpBillRCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<ProdMrpBillR> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, ProdMrpBillRCO paramObj) {
        return null;
    }
    
    @Override
    public void deleteRelateByMrpHeadCode(String mrpHeadCode) {
        String sql = "delete from prod_mrp_bill_r where mrp_head_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", mrpHeadCode);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public boolean isExistMrpLineRelateBill(String mrpHeadCode, String mrpLineCode, String billType) {
        String sql = "select count(*) from prod_mrp_bill_r r where r.mrp_head_code = :headcode and r.mrp_line_code = :linecode and r.bill_type = :billType";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("headcode", mrpHeadCode);
        args.put("linecode", mrpLineCode);
        args.put("billType", billType);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list.size()>0) {
            int count = this.daoSupport.convertSQLCount(list.get(0));
            if(count==0) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public List<ProdMrpBillR> getRelateByMrpHeadCode(String mrpHeadCode, String billType) {
        String sql = "select r.* from prod_mrp_bill_r r where r.mrp_head_code = :code and r.bill_type = :billType";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", mrpHeadCode);
        args.put("billType", billType);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("r", ProdMrpBillR.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
}
